﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;


namespace AppFuscator
{
    public partial class AuthForm : Form
    {
        private ApiRequest api = ApiRequest.Instance;
        private AppfuscatorConfig cfg = AppfuscatorConfig.Instance;

        public AuthForm()
        {
            InitializeComponent();
        }

        private bool Auth(string email, string password)
        {
            EnableUI(false);
            password = Utils.EncodeByMyAlgo(password);

            AuthStatus answer = api.Auth(email, password);
            if (answer == AuthStatus.Ok)
            {
                cfg.Login = email;
                cfg.Password = password;
                cfg.Save();

                tbEmail.Text = tbPassword.Text = "";
                EnableUI(true);
                return true;
            }
            else
            {
                string errorText = "";
                if (answer == AuthStatus.NotActive)
                    errorText = "User not activated. Please check e-mail";
                else if (answer == AuthStatus.NotFound)
                    errorText = "Authorization failed";
                else
                    errorText = "Unknown error.";

                MessageBox.Show(errorText, "Small problems");
            }
            EnableUI(true);
            tbEmail.Focus();

            return false;
        }

        public bool AutoAuth()
        {
            if (cfg.Login == "" || cfg.Password == "")
                return false;

            AuthStatus answer = api.Auth(cfg.Login, cfg.Password);
            if (answer == AuthStatus.Ok)
            {
                return true;
            }
            else
            {
                cfg.Login = "";
                cfg.Password = "";
                cfg.Save();
            }
            return false;
        }

        private void btnAuth_Click(object sender, EventArgs e)
        {
            if (Auth(tbEmail.Text, tbPassword.Text))
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

        private void AuthForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        private void EnableUI(bool state)
        {
            btnAuth.Enabled = state;
            btnRegister.Enabled = state;
            tbEmail.Enabled = state;
            tbPassword.Enabled = state;
            btnAuth.Focus();
        }

        private void lnkRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var lnk = (sender as LinkLabel).Text;
            Process.Start("http://" + lnk);
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            Process.Start("http://" + Utils.CabinetHost + "/register");
        }
    }
}
